import { observable, action } from "mobx"
import { getUserId } from '@/utils/utils';
import { network } from "@/utils/network"
import { IGood } from "./declare"


class SearchDataModel {

  @observable.ref
  hotKeywords: []

  @observable.ref
  historyKeywords: []

  @observable
  keyword: String


  @observable
  hasSearch: Boolean


  @observable.ref
  searchList: Array<Object>

  @observable.ref
  goods: IGood[]

  constructor() {
    this.hotKeywords = []
    this.historyKeywords = []
    this.searchList = []
    this.keyword = ''
    this.goods = []
    this.hasSearch = false
  }

  @action
  setValue({ hot, history }) {
    this.hotKeywords = hot;
    this.historyKeywords = history;
  }


  clearKeyword() {
    this.hasSearch = false;
    this.keyword = '';
    this.getData();
  }

  setKeyword(keyword: string) {
    this.keyword = keyword;
    this.searchKey();
  }


  async getData() {
    const { data } = await network.get('/xxm/keyword/list', {
      user_id: getUserId()
    });
    this.setValue(data);
  }

  async searchKey() {
    const { data } = await network.get('/xxm/search/key', {
      user_id: getUserId(),
      keyword: this.keyword
    });
    this.searchList = data;
  }

  async searchGood(keyword) {
    const { data } = await network.get('/xxm/search/goods', {
      user_id: getUserId(),
      keyword
    });
    this.keyword = keyword;
    this.hasSearch = true;
    this.goods = data;
  }


}

export const SearchModel = new SearchDataModel()
